import sys, os
import requests
from .dingding.dingding_demo import send_message
from .config import *

def read_firmwarre(TXT):
    global PATH
    TXT_path = os.path.join(PATH, TXT)
    if not os.path.exists(TXT_path):
        return []
    else:
        with open(TXT_path, 'r', encoding='utf-8') as f:
            firmware_list = f.readlines()
            return eval(firmware_list[0])

def re_write_firmware(firmware_list):
    global PATH
    TXT_path = os.path.join(PATH, 'all_miner_firmware_version.txt')
    with open(TXT_path,'w',encoding='utf-8') as f:
        f.write(str(firmware_list))

def judgement_firmware_version(new_firmware_list, firmware_list):
    return sorted(new_firmware_list) == sorted(firmware_list)


def get_url_html(url):
    headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) '
                             'Chrome/51.0.2704.63 Safari/537.36'}
    try:
        html = requests.get(url, headers=headers).text
        return html
    except:
        send_message("URL出现问题,请检查")
        sys.exit(0)